/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.cleber.dao;

import com.cleber.pojos.Usuario;
import com.cleber.util.HibernateUtil;
import java.security.MessageDigest;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Restrictions;

/**
 *
 * @author SantosSA
 */
public class UsuarioDao {
    
    Usuario usuario;
    Session sessao = null;

//    public void addUsuario(Usuario u) {
//
//        try {
//
//            sessao = HibernateUtil.getSessionFactory().openSession();
//
//            Usuario user = new Usuario();
//            user.setEmail(u.getEmail());
//            user.setNome(u.getNome());
//            user.setSenha(u.getSenha());
//
//            sessao.save(user);
//            sessao.beginTransaction().commit();
//
//        } catch (Exception e) {
//
//            e.printStackTrace();
//
//        } finally {
//
//            sessao.close();
//
//        }
//
//    }
    public void addUsuario(Usuario u) {

        try {

            sessao = HibernateUtil.getSessionFactory().openSession();

            Usuario user = new Usuario();
            user.setEmail(u.getEmail());
            user.setNome(u.getNome());

            MessageDigest algorithm = MessageDigest.getInstance("SHA-256");
            byte messageDigest[] = algorithm.digest(u.getSenha().getBytes("UTF-8"));

            StringBuilder hexString = new StringBuilder();
            for (byte b : messageDigest) {
                hexString.append(String.format("%02X", 0xFF & b));
            }
            String senha = hexString.toString();

            user.setSenha(senha);

            sessao.save(user);
            sessao.beginTransaction().commit();

        } catch (Exception e) {

            e.printStackTrace();

        } finally {

            sessao.close();

        }

    }

    public void removerUsuario(Usuario u) {

        try {

            sessao = HibernateUtil.getSessionFactory().openSession();

            sessao.delete(u);
            sessao.beginTransaction().commit();


        } finally {

            sessao.close();

        }

    }

    public List listarUsuarios() {

        try {

            sessao = HibernateUtil.getSessionFactory().openSession();
            Criteria criteria = sessao.createCriteria(Usuario.class);
            List<Usuario> user = criteria.list();

            return user;

        } finally {

            sessao.close();

        }

    }

    public boolean loginUsuario(Usuario u) {
        
        boolean retorno = false;
        Usuario user = null;
        
        try {

            sessao = HibernateUtil.getSessionFactory().openSession();



            MessageDigest algorithm = MessageDigest.getInstance("SHA-256");
            byte messageDigest[] = algorithm.digest(u.getSenha().getBytes("UTF-8"));

            StringBuilder hexString = new StringBuilder();
            for (byte b : messageDigest) {
                hexString.append(String.format("%02X", 0xFF & b));
            }
            String novaSenha = hexString.toString();

            Criteria criteria = sessao.createCriteria(Usuario.class);
            Criterion email = Restrictions.eq("email", u.getEmail());
            criteria.add(email);
            Criterion senha = Restrictions.eq("senha", novaSenha);
            criteria.add(senha);

            user = (Usuario) criteria.uniqueResult();

            
        } catch (Exception e) {

            e.printStackTrace();
        }
        
        if (user != null) {

                retorno = true;
                return retorno;

            }
        return retorno;

    }
}
